<template>
    <div>
        <!-- Form -->
        <el-button @click="dialogFormVisible = true">添加用户</el-button>
        <el-dialog title="用户信息" :visible.sync="dialogFormVisible" @close="closeDialog()">
            <el-form :model="user">
                <el-form-item label="账号" :label-width="formLabelWidth">
                    <el-input v-model="user.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="user.realname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" :label-width="formLabelWidth">
                    <el-input v-model="user.telephone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="头像" :label-width="formLabelWidth">
                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:8080/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="user.icon" :src="user.icon" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-input v-model="user.states" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveUser()">确 定</el-button>
            </div>
        </el-dialog>
        <el-table
                :data="users"
                style="width: 100%">
            <el-table-column
                    label="编号"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="账号"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.username }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="真实姓名"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.realname }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="手机号码"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.telephone }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="头像"
                    width="180">
                <template slot-scope="scope">
                    <img :src="scope.row.icon" style="width: 50px;height: 50px;border-radius: 50%">
<!--                    <span style="margin-left: 10px">{{ scope.row.icon }}</span>-->
                </template>
            </el-table-column>
            <el-table-column
                    label="状态"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.states }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-popconfirm
                            confirm-button-text='好的'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            @confirm="handleDelete(scope.$index, scope.row)"
                            title="确定删除吗？">
                        <el-button size="mini" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                :page-size="pageSize"
                @current-change="loadUserPage"
                :total="total">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "UserView",
        data(){
            return{
                users:[], //用户集合
                user:{} , //当前用户
                current: 1,     //当前页数
                pageSize: 10,    //分页长度
                total: 0,        // 数据总个数
                dialogFormVisible: false, //控制对话框的显示
                formLabelWidth: '120px'
            }
        },
        methods:{
            //分页查询
            loadUserPage(current){
                this.current = current;
                //调用后台分页查询接口
                this.axios.get("http://localhost:8080/users/page?current=" + this.current +"&pageSize=" + this.pageSize)
                    .then(result => {
                        if(result.data.status == "OK"){
                            //获得数据集合
                            this.users = result.data.data.records;
                            this.total = result.data.data.total;
                        }
                    });
            },
            //完成回显
            handleEdit(index, row) {
                console.log(index, row);
                this.user = JSON.parse(JSON.stringify(row));
                this.dialogFormVisible = true;
            },
            //删除
            handleDelete(index, row) {
                console.log(index, row);
                //后台删除用户
                this.axios.delete("http://localhost:8080/user/" + row.id)
                    .then(result => {
                        if(result.data.status == "OK"){
                            this.loadUserPage(this.current);
                        }
                    })
            },
            //添加或更新用户
            saveUser(){
                //通过用户id判断是否进行添加或更新
                if(this.user.id){
                    //存在id，就更新
                    this.axios.put("http://localhost:8080/user",
                        this.user)
                        .then(result => {
                            if(result.data.status == "OK"){
                                this.loadUserPage(this.current);
                            }
                        });
                }else{
                    //不存在id，就添加
                    this.axios.post("http://localhost:8080/user",
                        this.user)
                        .then(result => {
                            if(result.data.status == "OK"){
                                this.loadUserPage(this.current);
                            }
                        });
                }
                this.dialogFormVisible = false;
            },
            //上传成功回调
            handleAvatarSuccess(res, file) {
                console.log(res);
                this.user.icon = res.data;
            },
            //上传前检查
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //对话框关闭
            closeDialog(){
                this.user = {};
            }
        },
        mounted(){  //生命周期函数 界面挂载完成
            this.loadUserPage(1);
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>